import Vue from "vue";
import Router from "vue-router";
import store from "@/store";
import { dynamicRouter } from "@/router/dynamicRouter";

Vue.use(Router);

/* Layout */
import Layout from "@/layout";

export const routes = [
  {
    path: "/login",
    component: () => import("@/views/Login/index"),
    hidden: true,
  },
  {
    path: "/",
    component: Layout,
    redirect: "/workbench",
  },
  {
    path: "/workbench",
    component: Layout,
    children: [
      {
        path: "",
        component: () => import("@/views/Workbench/index"),
        meta: { title: "工作台", icon: "el-icon-data-board" },
      },
    ],
  },

  {
    path: "/cardAdd",
    component: () => import("@/views/Car/CarCard/cardAdd"),
    hide: true,
  },
  {
    path: "/roleEdit",
    component: () => import("@/views/System/Role/EditRole.vue"),
  },
  {
    path: "/addEnterprise",
    component: () => import("@/views/Park/Enterprise/AddEnterprise.vue"),
    hide: true,
  },
  {
    path: "/enterpriseDetail",
    component: () => import("@/views/Park/Enterprise/EnterpriseDetail.vue"),
  },
  {
    path: "/roleAdd",
    component: () => import("@/views/System/Role/AddRole.vue"),
  },
];

export function getFirstRouterPer(permissions) {
  const newArr = permissions.map((item) => {
    return item.split(":")[0];
  });
  return [...new Set(newArr)];
}
export function getSecondRouterPer(permissions) {
  const newArr = permissions.map((item) => {
    const arr = item.split(":");
    return `${arr[0]}:${arr[1]}`;
  });
  return [...new Set(newArr)];
}
//获取权限路由表功能函数

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    mode: "history",
    scrollBehavior: () => ({ y: 0 }),
    routes: routes,
  });

const router = createRouter();
export function getPermRouters(firstRouterPer, secondRouterPer) {
  let arr = dynamicRouter.filter((item) => {
    return firstRouterPer.includes(item.permission);
  });
  arr = arr.map((item) => {
    return {
      ...item,
      children: item.children.filter((item) =>
        secondRouterPer.includes(item.permission)
      ),
    };
  });
  return arr;
}
// 重置路由方法
export function resetRouter() {
  // 得到一个全新的router实例对象
  const newRouter = createRouter();
  // 使用新的路由记录覆盖掉老的路由记录
  router.matcher = newRouter.matcher;
}

router.beforeEach(async (to, from, next) => {
  // 登录页面直接放行
  if (to.path === "/login") {
    next();
  } else {
    const token = store.state.user.token;
    // 判断是否有token
    if (token) {
      // 放行
      next();

      const permission = await store.dispatch("user/getProfileAction");
      const firstRouterPer = getFirstRouterPer(permission);
      console.log("first", firstRouterPer);

      const secondRouterPer = getSecondRouterPer(permission);
      console.log("second", secondRouterPer);

      //管理员全部放行
      if (permission.includes("*:*:*")) {
        dynamicRouter.forEach((item) => {
          router.addRoute(item);
        });
        store.commit("menu/setRouter", dynamicRouter);
      } else {
        //获取权限路由表
        const permRouters = getPermRouters(firstRouterPer, secondRouterPer);
        
        permRouters.forEach((item) => {
          router.addRoute(item);
        });
        router.addRoute({
          path: "*",
          component: () => import("@/views/404"),
          hidden: true,
        });
        store.commit("menu/setRouter", permRouters);
      }
    } else {
      // 跳转回登录页
      next("/login");
    }
  }
});
export default router;
